﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events;
using myapp.Events.System;
using myapp.Events.ViewModel;
using myapp.Instances;
using myapp.ViewModels;
using myappcore.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;

namespace myapp.ViewModels
{
    [Export(typeof(IShell))]
    public class ShellViewModel : Screen, IShell, IHandle<CloseApplicationEvent>
    {
        #region  Attribute
        /// <summary>
        /// Window Manager
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// Event Aggregator
        /// </summary>
        private readonly IEventAggregator _eventAggregator;

        /// <summary>
        /// Logger
        /// </summary>
        private readonly ILog Logger = LogManager.GetLog(typeof(ShellViewModel));

        private Screen _detailFunctionViewModel;

        #endregion


        #region Properties

        /// <summary>
        /// Detail of function view
        /// </summary>
        public Screen DetailFunctionViewModel
        {
            get
            {
                return _detailFunctionViewModel;
            }
            private set
            {
                if (null != value)
                {
                    _detailFunctionViewModel = value;
                    NotifyOfPropertyChange(() => DetailFunctionViewModel);
                }
            }
        }

        #endregion

        #region Contructor
        /// <summary>
        /// Contructor
        /// </summary>
        /// <param name="windowManager"></param>
        /// <param name="eventAggregator"></param>
        [ImportingConstructor]
        public ShellViewModel(IWindowManager windowManager, IEventAggregator eventAggregator)
        {
            Logger.Info("Begin create " + this);

            DisplayName = "Shop Manager";
            this._windowManager = windowManager;
            this._eventAggregator = eventAggregator;
            this._eventAggregator.Subscribe(this);

            //Set MainInstance
            MainInstance.Instance.WindowManager = windowManager;
            MainInstance.Instance.EventAggregator = eventAggregator;

            //Init Plugin
            InitPlugin();

            //Init form layout
            _detailFunctionViewModel = new FunctionActiveViewModel();

            Logger.Info("End create " + this);
        }
        #endregion

        #region Method
        /// <summary>
        /// Init plugin
        /// </summary>
        private void InitPlugin()
        {
            foreach (Assembly assembly in AssemblySource.Instance)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsClass && typeof(IFunctionViewModel).IsAssignableFrom(type))
                    {
                        try
                        {
                            var function = Activator.CreateInstance(type) as IFunctionViewModel;
                            MainInstance.Instance.FunctionViewModelList.Add(function);
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex);
                        }
                    }
                }
            }

        }

        /// <summary>
        /// Close application event
        /// </summary>
        /// <param name="message"></param>
        public void Handle(CloseApplicationEvent message)
        {
            if (null != message)
            {
                this.TryClose();
            }
        }
        #endregion
    }
}
